package com.suket.jnn;

public class ExponentialFunction {

  /*
   * Compute the e^x using the Taylor series: e^x = 1 + x + x^2/2! + x^3/3! +
   * ... and also using Math.exp. Catastrophic cancelation occurs if x is a
   * large negative number.
   */

  public static double expTaylor( double x ) {
    double sum = 0.0;
    double term = 1.0;
    for ( int i = 1 ; sum != sum + term ; i++ ) {
      sum = sum + term;
      term = term * x / i;
    }
    return sum;
  }

  public static double expJava( double x ) {
    return Math.exp(x);
  }

}
